package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.contants.MessageConstant;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.ex.CustomerException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 员工业务实现
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    /**
     * 添加员工
     *
     * @param employee
     * @param employeeId
     */
    @Override
    public R add(Employee employee, Long employeeId) {
        // 判断员工用户名是否存在
        Employee emp = this.getOne(Wrappers.<Employee>lambdaQuery().eq(Employee::getUsername, employee.getUsername()));

        // 存在，返回错误结果
        if (!Objects.isNull(emp)) {
            // 抛出异常,程序中断
            throw new CustomerException(MessageConstant.EMPLOYEE_USERNAME_EXISTE);
        }
        // 补全实体属性：密码、状态、创建时间、更新时间、创建人、更新人
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);

        // 下面属性使用公共字段填充完成
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(employeeId);
        employee.setCreateUser(employeeId);*/
        //保存
        this.save(employee);
        System.out.println("--->" + Thread.currentThread().getId());
        return R.success(MessageConstant.EMPLOYEE_ADD_SUCCESS);
    }

    /**
     * 编辑员工
     *
     * @param employee
     * @param adminId
     */
    @Override
    public R modifyEmp(Employee employee, Long adminId) {
        // 参数补充
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(adminId);

        // 校验数据
        if (Objects.isNull(employee.getId())) {
            throw new CustomerException(MessageConstant.PARAM_ERROR);
        }

        // 修改员工状态
        if (StringUtils.isBlank(employee.getUsername())) {
            if (Objects.isNull(employee.getStatus()) && (employee.getStatus() != 0 && employee.getStatus() != 1)) {
                throw new CustomerException(MessageConstant.PARAM_ERROR);
            } else {
                this.updateById(employee);
            }
            return R.success(MessageConstant.EMPLOYEE_EDIT_STATUS_STATUS);
        }else {
            // 编辑员工信息
            // 判断当前用户名是否被使用
            Employee dbEmp = this.getOne(Wrappers.<Employee>lambdaQuery().eq(Employee::getUsername, employee.getUsername()));
            if (dbEmp == null || dbEmp.getId() == employee.getId()){
                this.updateById(employee);
            }else {
                return R.error(MessageConstant.EMPLOYEE_USERNAME_EXISTE);
            }
        }
        return R.success(MessageConstant.EMPLOYEE_EDIT_STATUS);
    }


}